import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { userList } from '#/api/system/user';
import { customerList } from '#/api/trade/customer';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  
  {
    component: 'Input',
    fieldName: 'contractNo',
    label: '合同号',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      api: () => customerList(),
      resultField: 'rows',
      labelField: 'name',
      valueField: 'id',
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().includes(input.toLowerCase());
      },
    },
    fieldName: 'customerId',
    label: '客户名称',
  },
  {
    component: 'Input',
    fieldName: 'contactPhone',
    label: '手机号',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      api: () => customerList(),
      resultField: 'rows',
      labelField: 'name',
      valueField: 'id',
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().includes(input.toLowerCase());
      },
    },
    fieldName: 'supplierId',
    label: '供应商',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      api: () => userList(),
      resultField: 'rows',
      labelField: 'nickName',
      valueField: 'userId',
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().includes(input.toLowerCase());
      },
    },
    fieldName: 'purchaseUserid',
    label: '采购人',
  },
 
  {
    component: 'Select',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.PURCHASE_CONTRACT_STATUS 便于维护
      options: getDictOptions('purchase_contract_status'),
    },
    fieldName: 'status',
    label: '合同状态',
  },
];

// 基础列配置
const baseColumns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '合同号',
    field: 'contractNo',
    width: 160,
    slots: {
      default: 'contractNo'
    },
  },
  {
    title: '合同日期',
    field: 'contractDate',
    width: 100,
    formatter: ({ cellValue }) => {
      if (!cellValue) return '';
      // 只显示年月日，不显示时分秒
      return cellValue.split(' ')[0];
    },
  },
  {
    title: '供应商',
    field: 'supplierName',
    width: 180,
  },
  {
    title: '联系人',
    field: 'contact',
  },
  {
    title: '联系方式',
    field: 'contactPhone',
  },
  {
    title: '销售数量',
    field: 'saleCount',
    width: 100,
  },
  {
    title: '合同总价',
    field: 'totalAmount',
    formatter: ({ cellValue }) => {
      if (!cellValue && cellValue !== 0) return '';
      return Number(cellValue).toLocaleString();
    },
  },

  {
    title: '采购人',
    field: 'purchaseUserName',
  },
  {
    title: '币种',
    field: 'currency',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.CURRENCY 便于维护
        return renderDict(row.currency, 'currency');
      },
    },
  },
  {
    title: '合同状态',
    field: 'status',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.PURCHASE_CONTRACT_STATUS 便于维护
        return renderDict(row.status, 'purchase_contract_status');
      },
    },
  },
];

// 审核人列配置
const approverColumn = {
  title: '审核人',
  field: 'approverUserName',
};

// 审核时间列配置
const approveTimeColumn = {
  title: '审核时间',
  field: 'updateTime',
  with:180
};

// 作废原因列配置
const rejectReasonColumn = {
  title: '作废原因',
  field: 'rejectReason',
};

// 根据状态动态生成列配置
export const getColumns = (status?: string): VxeGridProps['columns'] => {
  const columns = [...baseColumns];
  // 待审核状态不显示审核人和审核时间列
  if (status !== 'waiting') {
    columns.push(approverColumn);
    columns.push(approveTimeColumn);
  }
  
  // 只有已驳回和已作废状态显示作废原因列
  if (status === 'rejected' || status === 'invalid') {
    columns.push( rejectReasonColumn);
  }
  columns.push(
    {
      field: 'action',
      fixed: 'right',
      slots: { default: 'action' },
      title: '操作',
      width: 100,
    }
  );
 
  return columns;
};

// 默认列配置（保持向后兼容）
export const columns: VxeGridProps['columns'] = getColumns();

